﻿Imports System.ComponentModel
Imports System.Management
Public Class Form1
    Dim ipList As ArrayList = New ArrayList
    Dim ip As String = Nothing

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Me.lbComputers.DataSource = ipList.ToArray

        bgWorker.WorkerSupportsCancellation = True
    End Sub
    Private Sub btnPopulate_Click(sender As Object, e As EventArgs) Handles btnPopulate.Click
        If bgWorker.IsBusy = False Then
            bgWorker.RunWorkerAsync()
        Else
            bgWorker.CancelAsync()
            btnPopulate.Text = "Cancelling..."
        End If
        btnPopulate.Text = "Populating..."


    End Sub

    Private Sub bgWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
        populate()
    End Sub

    Private Sub bgWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged
        lblProgress.Text = "Populating list - " & e.ProgressPercentage & "% complete..."
        progress.Value = e.ProgressPercentage
        lbComputers.DataSource = ipList.ToArray

    End Sub

    Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
        btnPopulate.Text = "Repopulate list"
        lblProgress.Text = "Computers added " & ipList.Count
        lblProgress.Text = "Finished"

        lbComputers.DataSource = ipList
        progress.Value = 100
    End Sub

    Private Sub lbComputers_SelectedValueChanged(sender As Object, e As EventArgs) Handles lbComputers.SelectedValueChanged
        Try
            Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostByAddress(ipList.Item(lbComputers.SelectedIndex))
            tbHostName.Text = host.HostName

            Dim node0 As System.Windows.Forms.TreeNode = Nothing
            node0 = getNodeByName("Node0")
            node0.Text = host.HostName

        Catch ex As Exception
            tbHostName.Text = "NONAME"
        End Try


    End Sub

    Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnRun.Click
        Dim str As String = "\\jesse-w7 -u Pi -p raspberry C:\Windows\notepad.exe"
        tbHostName.Text = str
        Process.Start("C:\\pstools\\psexec.exe", tbHostName.Text)
    End Sub

    Sub populate()
        Dim min As Integer = 200
        Dim i As Integer = min
        Dim max As Integer = 212
        ip = "0"
        While i < max
            Dim temp As String = "192.168." & ip & "." & i
            bgWorker.ReportProgress((i - min) / (max - min) * 100)

            If My.Computer.Network.Ping(temp, 50) Then
                If ipList.Contains(temp) = False Then
                    ipList.Add(temp)
                End If
            End If
            i += 1
        End While
    End Sub

    Function getNodeByName(name As String)
        Return tree.Nodes.Find(name, False).First()
    End Function

    Private Sub btnLogOff_Click(sender As Object, e As EventArgs) Handles btnLogOff.Click
        Dim str As String = "\\jesse-w7 -u Pi -p raspberry C:\Windows\shutdown.exe -l"
        tbHostName.Text = str
        Process.Start("C:\\pstools\\psexec.exe", tbHostName.Text)
    End Sub

    Private Sub tree_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles tree.AfterSelect

    End Sub
End Class
